/**
 * 
 */
package com.imonsoft.common.servlet;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.beanutils.converters.SqlTimestampConverter;
import org.apache.commons.beanutils.converters.StringConverter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 初始化基础数据，将公共信息加入服务器内存
 * 
 * @author LiQianHui
 * @date 2013-04-17
 * @version 2013
 */
public class InitBaseDataServlet extends HttpServlet {

	private static final long serialVersionUID = -7507001443202462310L;

	private final Log logger = LogFactory.getLog(getClass());

	private boolean initCache = false;

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);

		//
		registConverter();

		logger.info("开始初始化基础数据......");
		long startTime = System.currentTimeMillis();

		if (initCache) {

		}
		long endTime = System.currentTimeMillis();
		logger.info("基础数据完成初始化！(" + ((endTime - startTime) / 1000.0) + "s)");
	}

	private void registConverter() {
		ConvertUtils.register(new StringConverter(), String.class);
		ConvertUtils.register(new IntegerConverter(null), Integer.class);
		ConvertUtils.register(new LongConverter(null), Long.class);
		ConvertUtils.register(new FloatConverter(null), Float.class);
		ConvertUtils.register(new DoubleConverter(null), Double.class);
		ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
		String[] patterns = { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH", "yyyy-MM-dd" };
		DateConverter dateConverter = new DateConverter(null);
		dateConverter.setPatterns(patterns);
		ConvertUtils.register(dateConverter, Date.class);
		SqlTimestampConverter sqlTimestampConverter = new SqlTimestampConverter(null);
		sqlTimestampConverter.setPatterns(patterns);
		ConvertUtils.register(sqlTimestampConverter, Timestamp.class);
	}
}
